var stream = weex.requireModule('stream');


var url = {
    android:'http://gank.io/api/data/Android/10/',
    ios:'http://gank.io/api/data/iOS/10/',
    beauty:'http://gank.io/api/data/福利/10/',
    app:'http://gank.io/api/data/App/10/',
    random:'http://gank.io/api/random/data/福利/1',
};

function get(url,callback){
    console.log('[VUE LOG] apiUrl:'+ url);
    stream.fetch({
        method:'GET',
        url:url,
        type: 'json',
    },function(ret){
        // console.log('apiRet: '+JSON.stringify(data));
        if(ret.status == 200){
            var data = ret.data;
            if(!data.error){
                callback(data.results);
            }
        }

    });
};

function getResultMap(url,callback){
    console.log('[VUE LOG] apiUrl:'+ url);
    stream.fetch({
        method:'GET',
        url:url,
        type: 'json',
    },function(ret){
        console.log('apiRet: '+JSON.stringify(ret));
        if(ret.status == 200){
            var data = ret.data;
            callback(data.resultMap.data);
        }else{
            console.log('[VUE LOG] status:'+ ret.status+" statusText:"+ret.statusText);
        }
    });
};

function post(url,callback){
    stream.fetch({
        method:"POST",
        type:'json',
        url:url,
        headers:{'Content-Type':'application/x-www-form-urlencoded'},
        // body:config.toParams({
        //     type:'shentong',
        //     postid:'3333557693903',
        // })
    }, function(ret) {
        if(!ret.ok){
            console.log('post失败:'+JSON.stringify(ret));
            // console.log('post---------:'+JSON.stringify(ret.data));
            // modal.toast({
            //     'message': "失败",
            //     'duration': 2.0
            // })
        }else{
            console.log('post---------:'+JSON.stringify(ret.data));
            callback(ret.data.resultMap.data);
        }
    },function(progress) {
//                    JSON.stringify(progress.length);
    })
}

function postWithParams(url,params,callback){
    stream.fetch({
        method:"POST",
        type:'json',
        url:url,
        headers:{'Content-Type':'application/json; charset=utf-8'},
        body:JSON.stringify(params)
    }, function(ret) {
        if(!ret.ok){
            console.log('post失败:'+JSON.stringify(ret));
            // modal.toast({
            //     'message': "失败",
            //     'duration': 2.0
            // })
        }else{
            console.log('post---------:'+JSON.stringify(ret.data));
            callback(ret.data);
        }
    },function(progress) {
//                    JSON.stringify(progress.length);
    })
}

exports.android = function(params,callback){
    get(url.android+params,callback);
};

exports.ios = function(params,callback){
    get(url.ios + params, callback);
};

exports.beauty = function(params,callback){
    get(url.beauty + params, callback);
};

exports.app = function(params,callback){
    get(url.app + params,callback);
};

exports.random = function(callback){
    get(url.random, callback);
};

exports.getData = function(url,callback){
    get(url,callback);
}

exports.doGet = function(url,callback){
    getResultMap(url,callback);
}

exports.doPost = function(url,callback){
    post(url,callback);
}

exports.doPostWithParams = function(url,params,callback){
    postWithParams(url,params,callback);
}